<?php

namespace App\Models;

// use Illuminate\Contracts\Auth\MustVerifyEmail;
use App\Traits\HasDateTimeFormatter;
use App\Traits\HasOrgCode;
use App\Traits\HasOrgRoot;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;

class User extends Authenticatable
{
    use HasApiTokens, HasFactory, Notifiable, HasOrgCode, HasOrgRoot, HasDateTimeFormatter;

    /**
     * The attributes that are mass assignable.
     *
     * @var array<int, string>
     */
    protected $fillable = [
        'name',
        'mobile',
        'password',
    ];

    /**
     * The attributes that should be hidden for serialization.
     *
     * @var array<int, string>
     */
    protected $hidden = [
        'password',
        'remember_token',
    ];

    protected $appends = ['status_name', 'gender_name', 'kyc_name'];

    protected $casts =[
        'status' => 'integer',
        'gender' => 'integer',
        'is_manager' => 'integer',
        'kyc' => 'integer',
        'parent_id' => 'integer',
        'invite_count' => 'integer',
        'money' => 'float',
        'ticket' => 'integer',
    ];

    /**
     * 是否超级管理员, 没有配置就默认ID为1的用户
     * @return bool
     */
    public function isSupper() {
        $mobiles = env('ADMIN_MOBILE', null);
        if(empty($mobiles)) return  $this->attributes['id'] == 1;

        $moible_arr = array_map(fn($v)=>trim($v),  explode(",", trim($mobiles)));

        return in_array($this->attributes['mobile'], $moible_arr);
    }

    /**
     * @return bool
     */
    public function isRootManager() {
        return $this->attributes['is_manager'] &&
            !empty($this->attributes['org_root']) &&
            $this->attributes['org_root'] == $this->attributes['org_code'];
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     */
    public function parent() {
        return $this->hasOne(User::class, 'id', 'parent_id');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     */
    public function org() {
        return $this->hasOne(Org::class, 'org_code', 'org_code');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     */
    public function orgRoot() {
        return $this->hasOne(Org::class, 'org_code', 'org_root');
    }

    /**
     * @return Attribute
     */
    protected function genderName(): Attribute
    {
        return Attribute::make(
            get: function ($value, $attributes) {
                $define = [0=>'未知', 1=>"男", 2=>"女"];
                return $define[$attributes['gender']] ?? $define[0];
            },
        );
    }

    /**
     * 资金流水
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function moneyLogs()
    {
        return $this->hasMany(UserMoneyLog::class, 'user_id');
    }

    /**
     * 资金流水
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function ticketLogs()
    {
        return $this->hasMany(UserTicketLog::class, 'user_id');
    }

    /**
     * 邀请人
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function invites()
    {
        return $this->hasMany(UserInvite::class, 'user_id');
    }

    /**
     * 认证详情
     */
    public function kycInfo()
    {
        return $this->hasOne(UserKyc::class, 'user_id');
    }

    /**
     * @return Attribute
     */
    protected function statusName(): Attribute
    {
        return Attribute::make(
            get: function ($value, $attributes) {
                $define = [0=>"未知", 1=>'正常', 2=>"休假", 3=>"离职"];
                return $define[$attributes['status']] ?? $define[0];
            },
        );
    }

    /**
     * @return Attribute
     */
    protected function kycName(): Attribute
    {
        return Attribute::make(
            get: function ($value, $attributes) {
                $define = [-1 => "认证失败", 0=>"未认证", 1=>'审核中', 2=>"认证成功"];
                return $define[$attributes['kyc']] ?? $define[0];
            },
        );
    }

    /**
     * 获取可用金额
     */
    protected function freeMoney(): Attribute
    {
        return Attribute::make(
            get: function ($value, $attributes) {
                return  (float)$attributes['money'] - (float) UserMoneyWithdraw::lockedMoney($attributes['id']);
            },
        );
    }
}
